<template>
	<view>
		<pageheader :setting="settingdata"></pageheader>
		<view class="_sticky" :style="'top:'+navH+'px'">
			<view class="searchTop">
				<view class="searchContent">
					<u-icon name="search" color="#B1B8C8" size="28" class="search_icon" @click="getclass"></u-icon>
					<u-input v-model="searchvalue" placeholder="输入搜索内容" type="text" confirm-type="search"
						:border="false" placeholderStyle="color:#B1B8C8" class="search_" :clearable="false"
						@confirm="getclass" @input="bindchange" />
					<u-icon name="close-circle" color="#B1B8C8" size="28" class="clo_icon" @click="reset()"
						v-if="searchvalue!=''"></u-icon>
				</view>
				<u-button type="success" size="mini" :ripple="true" shape="circle" @click="reset()" throttle-time="1000">
					重置</u-button>
			</view>
			<!-- <view class="choosetext">请选择分类</view> -->
		</view>

		<view v-if="searchvalue==''">
			<view class="hotstitlebox">
				<text class="hotstitle">热门搜索</text>
			</view>
			<view class="searchvalues">
				<view class="searchone" v-for="(item,index) in hotsdata" :key="index" @click="setinput(item.title)">
					{{item.title}}
				</view>
			</view>
		</view>
		<!-- <view class="alllist" v-if="!selectswitch">
			<block v-for="(item,index) in ification" :key="index">
				<view class="listone" :class="classopt==index?'liston':''" @click="shows(index)">
					<text>{{item.name}}</text>
					<u-icon :name="classopt==index?'minus':'plus'" color="#CFD2D1"
						:class="classopt==index?'#38BE8C':'#CFD2D1'" size="28"></u-icon>
				</view>
				<view class="listcontent" v-if="classopt==index">
					<view class="listtwo" v-for="(item2,index2) in item.list" :key="index2"
						@click="setparent(item2.id,item2.name)">{{item2.name}}</view>
				</view>
			</block>
		</view> -->
		<view class="alllist" v-if="searchvalue!=''">
			<scroll-view scroll-y="true" class="scroll" @scrolltolower="lower"
				:style="'height:'+'calc(100vh'+' - '+(navH+70)+'px)'">
				<view class="towline" v-for="(item,index) in ification" :key="index"
					@click="setparent(item.id,item.name,item.s_name)">
					{{item.name}}
				</view>
			</scroll-view>
		</view>
		<!-- <view class="not_more" v-if="ification.length<1">
			<image src="../../static/img/wsousuo.png" mode="widthFix"></image>
			<view class="text">暂无类型</view>
		</view> -->
	</view>
</template>

<script>

	var _this, $paginator;
	export default {
		data() {
			return {
				settingdata: {
					name: '选择分类',
					bgColor: '#fff',
					titleColor: '#000000',
					showNav: true,
					showTypes: 0,
					position: "choosecatgory"
				},
				navH: 0,
				searchvalue: '',
				list: [0, 0, 0, 0, 0, 0, 0, 0],
				// 分类列表
				ification: [],
				classopt: -1,
				selectswitch: false,
				hotsdata: [], //热门搜索
			}
		},
		onLoad(e) {
			_this = this
			
			_this.gethot()
		},
		methods: {
			// 显示分类
			getclass: function(firstLoad = false) {
				$paginator.getData("classification", {
					key: _this.searchvalue,
					pagesize: 20
				}, (data) => {
					if (firstLoad) {
						_this.ification = data.list ? data.list : []
					} else {
						_this.ification = _this.ification.concat(data.list)
					}
					console.log("分类",_this.ification)
				});
				if (_this.searchvalue.length > 0) {
					_this.selectswitch = true
				} else {
					_this.selectswitch = false
				}
			},

			// 滚动到底部
			lower: function(e) {
				console.log(e)
				$paginator.nextPager(() => {
					_this.getclass()
				})
			},

			// 获取数据
			gethot() {
				_this.$api.get("gethotsearch", {}).then(data => {
					_this.hotsdata = data
				});
			},

			reset() {
				_this.ification = []
				_this.selectswitch = false
				_this.searchvalue = ""
			},
			setparent(id, name,s_name) {
				uni.setStorageSync("cate", {
					id: id,
					name: name,
					s_name:s_name
				})
				uni.navigateBack({
					delta: 1
				})
			},
			setinput(value) {
				_this.searchvalue = value
				$paginator = new _this.$paginator(_this.$api)
				_this.getclass(true)
			},
			shows(e) {
				console.log(this.classopt, e)
				this.classopt = this.classopt == e ? -1 : e
				console.log(this.classopt, e)
			},

			bindchange(e) {
				console.log(e, e == "")
				_this.searchvalue = e
				if (e == "") {
					_this.ification = []
				} else {
					$paginator = new _this.$paginator(_this.$api)
					_this.getclass(true)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	._sticky {
		position: sticky;
		background: #fff;
		z-index: 9;

		.searchTop {
			display: flex;
			flex-direction: row;
			align-items: center;
			padding: 25rpx 20rpx;
			height: 100rpx;

			.searchContent {
				width: 698rpx;
				height: 68rpx;
				border-radius: 34rpx;
				background: #F0F0F0;
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-right: 28rpx;

				.search_icon {
					margin-left: 32rpx;
				}

				.search_ {
					color: #222222;
					font-size: 28rpx;
					flex: 1;
					margin: 0 14rpx;
				}

				.clo_icon {
					margin-right: 32rpx;
				}
			}
		}

		.choosetext {
			margin: 0 51rpx;
			padding: 37rpx 0 28rpx 0;
			color: #999999;
			font-size: 28rpx;
		}
	}

	.alllist {
		margin: 0 25rpx;
		display: flex;
		flex-direction: column;

		.scroll {
			// height: 900rpx;
		}

		.listone {
			width: 100%;
			height: 88rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			padding: 0 25rpx;
			margin-top: 4rpx;
			background-color: #F8F8F8;

			text {
				color: #222222;
				font-size: 30rpx;
			}
		}

		.liston {
			background-color: #EBF8F3;

			text {
				color: #38BE8C;
			}
		}

		.listcontent {
			padding: 0 20rpx 46rpx 0;
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;

			.listtwo {
				margin: 47rpx 0 0 47rpx;
			}
		}

		.towline {
			padding: 30rpx;
			border-bottom: 2rpx solid #E6E6E6;
			color: #222222;
			font-size: 30rpx;
		}
	}

	.searchvalues {
		margin-top: 16rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;

		.searchone {
			padding: 0 59rpx;
			height: 70rpx;
			border-radius: 35rpx;
			line-height: 70rpx;
			background-color: #EEEEEE;
			font-size: 26rpx;
			color: #2C3240;
			margin: 19rpx 0 0 20rpx;
		}
	}

	.hotstitlebox {
		margin-top: 30rpx;
		padding: 0 28rpx;

		.hotstitle {
			color: #2C3240;
			font-size: 32rpx;
			font-weight: bold;
		}
	}
</style>
